// utils/eventBus.js

const eventBus = {
  events: {}, // 存储事件和回调函数

  /**
   * 注册事件监听器
   */
  on(eventName, callback) {
    if (!this.events[eventName]) {
      this.events[eventName] = [];
    }
    this.events[eventName].push(callback);
  },

  /**
   * 触发事件
   */
  emit(eventName, data) {
    if (this.events[eventName]) {
      this.events[eventName].forEach(callback => callback(data));
    }
  },

  /**
   * 移除事件监听器
   */
  off(eventName, callback) {
    if (this.events[eventName]) {
      this.events[eventName] = this.events[eventName].filter(cb => cb !== callback);
    }
  }
};

module.exports = eventBus;